<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/12/2
 * Time: 12:16
 */
namespace app\models;

use think\Model;

/**
 * Class TeacherModel
 * @package app\models
 */
class CapitalRecordListModel extends Model
{
    protected $table = 'edu_allocation_record';

    /**
     * @param $param
     * @return array
     * @throws \think\exception\DbException
     */
    public function getCapitalRecordListLists($param,$contract_number)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = ['campus' => get_current_campus_id()];
        if (isset($param['contracted_name']) && '' !== $param['contracted_name']) {
            $where['contracted_name'] = ['like', $param['contracted_name'].'%'];
        }

        $listData = self::where(["contract_number" => $contract_number])
            ->order('create_time desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();

        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {

                $item['campus_name'] =  CampusModel::get(["id" => $item['campus_id']])['campus_name'];
                $item['allocation_fund'] =  $item['allocation_fund'] / 100;
                $item['distributive_person_text'] = UserModel::get(['id' => $item['distributive_person']])['name'];
                if($item['project_type'] == 1){  //课程
                    $course_id = CourseContractModel::get(['id' => $item['project_id']])['course_id'];
                    $item['project_name'] = CourseModel::get(['id' => $course_id])['course_name'];
                }else if($item['project_type'] == 2){  //班级
                    $grades_id = GradesContractModel::get(['id' => $item['project_id']])['grades_id'];
                    $item['project_name'] = GradesDetailsModel::get(['id' => $grades_id])['grades_name'];
                }
            }
        }

        return $listData;
    }


    public function getCapitalRecordAllListLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        if (isset($param['contract_number']) && '' !== $param['contract_number']) {
            $where['contract_number'] = ['like', $param['contract_number'].'%'];
        }

        //校区
        if (isset($param['campus_id']) && '' !== $param['campus_id']) {
            $where['campus_id'] = $param['campus_id'];
        }else{
            if(get_current_type() == 4 || get_current_type() == 5){
                $where['campus_id'] = get_current_campus_id();
            }elseif (get_current_type() == 2 || get_current_type() == 3){
                $where['campus_id'] = ['in',get_current_campus_id()];
            }
        }

        //时间段
        if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
            $where['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
        }


        $listData = self::where($where)->order('create_time desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();

        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $item['campus_name'] =  CampusModel::get(["id" => $item['campus_id']])['campus_name'];//分配校区
                $item['allocation_fund'] =  $item['allocation_fund'] / 100;
                if($item['allocation_fund'] >= 0){  //分配
                    $item['type'] = '<span  style="color:#01AAED;font-weight: bold;">资金分配</span>';
                }
                if($item['allocation_fund'] < 0){   //回抽
                    $item['type'] = '<span  style="color:#FF5722;font-weight: bold;">资金回抽</span>';
                }

                if($item['project_type'] == 1){
                    $item['project_type_text'] = "课程";  //项目分类
                    $res = CourseContractModel::get(['id' => $item['project_id']]);
                    $res_course = CourseModel::get(['id' => $res['course_id']]);
                    $item['project_name'] = $res_course['course_name'];
                    $item['project_class_type'] = DictionaryModel::get(['id' => $res_course['course_type']])['name'];
                }else if($item['project_type'] == 2){
                    $item['project_type_text'] = "班级";  //项目分类
                    $res = GradesContractModel::get(['id' => $item['project_id']]);
                    $item['project_name'] = GradesDetailsModel::get(['id' => $res['grades_id']])['grades_name'];
                    $res_course = CourseModel::get(['id' => $res['course_id']]);
                    $item['project_class_type'] = DictionaryModel::get(['id' => $res_course['course_type']])['name'];
                }
                $item['contractor'] = UserModel::get(["id" => $item['contractor']])['name']; //签约人
                $item['distributive_person'] = UserModel::get(["id" => $item['distributive_person']])['name']; //分配人


            }
        }

        return $listData;
    }


}